package cn.superchart.module.uerole.service.impl;

import cn.superchart.common.base.BaseServiceImpl;
import cn.superchart.common.utils.Checker;
import cn.superchart.module.role.param.ScRoleDetailParam;
import cn.superchart.module.role.param.ScRoleEditParam;
import cn.superchart.module.uerole.entity.ScUserRole;
import cn.superchart.module.uerole.mapper.ScUserRoleMapper;
import cn.superchart.module.uerole.param.ScUserRoleAddParam;
import cn.superchart.module.uerole.param.ScUserRoleDetailParam;
import cn.superchart.module.uerole.param.ScUserRoleEditParam;
import cn.superchart.module.uerole.service.ScUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ScUserRoleServiceImpl extends BaseServiceImpl<ScUserRole, ScUserRoleMapper> implements ScUserRoleService {

    @Override
    public void add(ScUserRoleAddParam scUserRoleAddParam) {
        String pk = super.saveData(scUserRoleAddParam);
        log.info(pk);
    }

    @Override
    public void edit(ScUserRoleEditParam scUserRoleEditParam) {
         super.updateData(scUserRoleEditParam);
    }

    @Override
    public ScUserRoleDetailParam detail(String id) {
        return queryData(id,ScUserRoleDetailParam.class);
    }

    @Override
    public void initUserRole(String userId, String roleId) {
        ScUserRole scUserRole = new ScUserRole();
        scUserRole.setUserId(userId);
        scUserRole.setRoleId(roleId);
        super.save(scUserRole);
    }

    @Override
    public void removeByUid(String userId) {
        LambdaQueryWrapper<ScUserRole> queryWrapper =  queryWrapper().lambda().eq(ScUserRole::getUserId,userId);
        super.remove(queryWrapper);
    }

    @Override
    public List<String> queryRoleIdByUid(String userId) {
        List<String> roleIds = baseMapper.queryRoleIdsByUid(userId);
        if(Checker.BeNotEmpty(roleIds)){
            return roleIds;
        }
        return Lists.newArrayList();
    }

}
